package org.example.SolutionNew006;

public class BinarySearch004 {
    public static void main(String[] args) {
        BinarySearch004 binarySearch004 = new BinarySearch004();
        int[] nums = new int[]{3,1};
        System.out.println(binarySearch004.search(nums, 1));

    }

    public int search(int[] nums, int target) {
        //1.将排序恢复，然后二分查找
        //2.分段有序，比较即可
        int left=0,right=nums.length-1;
        while(left<=right){
            int mid = left+(right-left)/2;
            if(nums[mid]==target){
                return mid;
            }else{
                if(nums[left]<=nums[mid]){
                    if(nums[left]<=target&&nums[mid]>target){
                        right=mid-1;
                    }else{
                        left=mid+1;
                    }
                }else{
                    //mid<right
                    if(nums[mid]<target&&nums[right]>=target){
                        left=mid+1;
                    }else{
                        right=mid-1;
                    }
                }
            }
        }
        return -1;
    }
}
